package com.zsl.swing.redis.desktop.window.help;

import com.zsl.swing.redis.desktop.model.FontSizeConfigEntity;
import com.zsl.swing.redis.desktop.utils.FontUtils;

import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import java.awt.*;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Objects;

public class ZslCmdTable extends JTable {

    private ZslRedisCmdHelpWindow parent;

    private int column;

    private int row;

    public ZslCmdTable(FontSizeConfigEntity fontConfig, ZslRedisCmdHelpWindow parentWindow) {
        super(new ZslTableModel());

        this.parent = parentWindow;

        this.setFont(FontUtils.getFont(fontConfig.getFontName(), Font.PLAIN, fontConfig.getWindow()));

        this.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);

        this.setRowSelectionAllowed(false);

        this.overrideDefaultCopyAction();
        
        
        this.setCellWidthAndHeight();

        this.repaint();

    }

    private void setCellWidthAndHeight() {

        this.setRowHeight(50);

        int width = this.parent.getWidth();


        this.getColumnModel().getColumn(0).setMinWidth(width * 8 / 15);
        this.getColumnModel().getColumn(1).setMinWidth(width * 5 / 15);
        this.getColumnModel().getColumn(2).setMinWidth(width * 1 / 15);
        this.getColumnModel().getColumn(3).setMinWidth(width * 1 / 15);


        this.getColumnModel().getColumn(0).setCellRenderer(new LineWrapCellRender(this));
        this.getColumnModel().getColumn(1).setCellRenderer(new LineWrapCellRender(this));
        this.getColumnModel().getColumn(2).setCellRenderer(new LineWrapCellRender(this));
        this.getColumnModel().getColumn(3).setCellRenderer(new LineWrapCellRender(this));

        this.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                ZslCmdTable.this.column = ZslCmdTable.this.getSelectedColumn();
                ZslCmdTable.this.row = ZslCmdTable.this.getSelectedRow();
            }
        });

    }

    private void overrideDefaultCopyAction() {
        
        this.getActionMap().put("copy", new AbstractAction() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JTable table = (JTable)e.getSource();
                int row = table.getSelectedRow();
                int column = table.getSelectedColumn();
                Object value = table.getValueAt(row, column);

                // copy the data to the clipboard
                if(!Objects.isNull(value)){
                    Clipboard c = Toolkit.getDefaultToolkit().getSystemClipboard();
                    StringSelection testData = new StringSelection( value.toString() );
                    c.setContents(testData, testData);
                }


            }
        });
    }

    public void refreshTable(String keyword, String group) {
        System.out.println("组：" + group + "，命令：" + keyword );

        ((ZslTableModel)this.getModel()).refreshModel(keyword, group);

        this.scrollRectToVisible(this.getCellRect(0,0, false));

        this.repaint();
    }


    private static class LineWrapCellRender extends JTextArea implements TableCellRenderer {

        private ZslCmdTable cmdTable;

        public LineWrapCellRender(ZslCmdTable table){
            super(2,50);
            this.setLineWrap(true);
//            this.setWrapStyleWord(true);

            this.cmdTable = table;
        }

        @Override
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

            this.setText(value.toString());
            this.setSize(table.getColumnModel().getColumn(column).getWidth(), this.getPreferredSize().height );
            this.setLineWrap(true);
            if (table.getRowHeight(row) != getPreferredSize().height) {
                table.setRowHeight(row, getPreferredSize().height);
            }

            if(this.cmdTable.column == column && this.cmdTable.row == row){
                this.setBackground(new Color(240, 255, 255));
            }else{
                this.setBackground(new Color(255, 235 , 205));
            }
            return this;
        }
    }
}
